using System.Linq;
using System.Collections.Generic;

namespace PokerStar.Core.Rules
{
    public class FlushRule : BasicRule
    {
        public FlushRule(IRule next)
            : base(next)
        {
        }

        protected override GameResult Match(RuleContext context)
        {
            //foreach (var group in cards.GroupBy(p => p.Suit))
            //{
            //    if (group.Count() >= 5)
            //    {
            //        var rs = new FlushGameResult(group.Key);
            //        rs.Cards = group.OrderByDescending(p => p.Number, new CardNumberComparer()).Take(5).ToList();
            //        rs.Evidence = rs.Cards.Select(p => CardNumberComparer.Card2Int(p.Number)).ToList();
            //        return rs;
            //    }
            //}

            if (context.GroupBySuit.First().Count() >= 5)
            {
                var group = context.GroupBySuit.First();
                var rs = new FlushGameResult(group.Key);
                rs.Cards = group.OrderByDescending(p => p.Number, new CardNumberComparer()).Take(5).ToList();
                rs.Evidence = rs.Cards.Select(p => CardNumberComparer.Card2Int(p.Number)).ToList();
                return rs; 
            }

            return null;
        }
    }

    public class FlushGameResult : GameResult
    {
        public FlushGameResult(Suit suit)
        {
            Suit = suit;
        }

        public Suit Suit { get; private set; }

        public override string ToString()
        {
            return string.Format("Flush ({5}) -> {0} {1} {2} {3} {4}", Cards[0], Cards[1], Cards[2], Cards[3], Cards[4], (char)Suit);
        }

        public override ResultLevel Level
        {
            get { return ResultLevel.Flush; }
        }
    }
}